package com.zyf.springcloud.service.impl;

import com.alibaba.excel.EasyExcel;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zyf.springcloud.dao.ListDao;
import com.zyf.springcloud.model.List;
import com.zyf.springcloud.model.ListExcelData;
import com.zyf.springcloud.result.Result;
import com.zyf.springcloud.service.ListService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;

@Service
public class ListServiceImpl extends ServiceImpl<ListDao, List> implements ListService {
    @Autowired
    ListDao listDao;
    //把数据打印到excel
    public Result getExcel(){

        java.util.List<ListExcelData> data=new ArrayList<>();
      java.util.List<List> list=  listDao.selectList(null);
      for(List item:list){
          ListExcelData listExcelData=new ListExcelData(
                  item.getOnlyId(),
                  item.getId(),
                  item.getParentId(),
                  item.getUserName(),
                  item.getTrueName(),
                  item.getCategoryName()
          );
          data.add(listExcelData);
      }
      String fileName="D:\\listTable.xlsx";
        EasyExcel.write(fileName,ListExcelData.class).sheet("用户信息").doWrite(data);
        return Result.ok();
    }

    //给Children添加数据
    public void getChildren(List list) {
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("parent_id", list.getId());
        java.util.List<List> listList = listDao.selectList(queryWrapper);
        list.setChildren(listList);
        if (listList == null) {
            //空
        } else {
            for (List list1 : listList) {
                //便利
                getChildren(list1);
            }
        }
    }

    public Result getCategory() {
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("parent_id", "0");
        java.util.List<List> list = listDao.selectList(queryWrapper);
        list.forEach(System.out::println);
        return Result.ok(list);
    }

    public Result getMyId(int id) {
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("id", id);
        return Result.ok(listDao.selectOne(queryWrapper));
    }

    public java.util.List<List> getAll() {
        return listDao.selectList(null);
    }
}
